﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NCRGEA.UserControls
{
    public partial class ucPrograms : UserControl
    {
        bool _IsEnrolled = false;
        private int _MemberID;
        public int MemberID
        {
            get
            {
                return _MemberID;
            }
            set
            {
                _MemberID = value;
                RefreshData();
            }
        }

        private bool _IsEditable;
        public bool IsEditable
        {
            get
            {
                return _IsEditable;
            }
            set
            {
                _IsEditable = value;
                UpdateEditable();
            }
        }

        private void UpdateEditable()
        {
            grdPrograms.Enabled = _IsEditable;
            btnAddProgram.Enabled = _IsEditable;
            btnDelete.Enabled = _IsEditable;
            this.Enabled = _IsEditable;

            if (_IsEditable)
                UpdateUI();
        }

        public ucPrograms()
        {
            InitializeComponent();
        }

        private void UpdateUI()
        {
            _IsEnrolled = AAL.UAL.IsMemberEnrolled(_MemberID);
            grdPrograms.Enabled = _IsEnrolled;
            btnAddProgram.Enabled = _IsEnrolled;
            btnDelete.Enabled = _IsEnrolled;
            this.Enabled = _IsEnrolled;

            if (_IsEnrolled)
            {
                if (((DataTable)grdPrograms.DataSource).Rows.Count == 0)
                    btnDelete.Enabled = false;
            }
        }


        private void RefreshData()
        {
            string sqlQry = @"SELECT
	                                pl.PlanID,
	                                pl.ProgramID,
	                                pl.StartDate [Effective Date],
	                                pr.Amount,
	                                pr.Description
                                FROM
	                                memPlans pl
	                                INNER JOIN
		                                memPrograms pr
	                                ON
		                                pl.ProgramID=pr.ProgramID
                                WHERE
	                                pl.MemberID=@MemberID AND
	                                pl.Active=1";
            SqlParameter[] sParamArray = new SqlParameter[1];
            sParamArray[0] = new SqlParameter("@MemberID", _MemberID) { SqlDbType = SqlDbType.Int };
            DataTable result = AAL.DAL.ExecuteQuery(sqlQry, sParamArray);
            if (result != null)
                grdPrograms.DataSource = result;
            UpdateUI();
            ValidateAddButton();
        }

        private void ValidateAddButton()
        {
            if (btnAddProgram.Enabled == false)
                return;
            string sqlQry = @"SELECT
	                                ProgramID,
	                                Description,
                                    '$' + CAST(CAST(Amount as NUMERIC(10,2)) as VARCHAR) Amount
                                FROM
	                                memPrograms
                                WHERE
	                                ProgramTypeID NOT IN
		                                (SELECT
			                                pr.ProgramTypeID
		                                FROM
			                                memPlans pl
			                                INNER JOIN
				                                memPrograms pr
			                                ON
				                                pl.ProgramID=pr.ProgramID
		                                WHERE
			                                pl.MemberID=@MemberID AND
			                                pl.Active=1)";
            SqlParameter[] sParamArray = new SqlParameter[1];
            sParamArray[0] = new SqlParameter("@MemberID", _MemberID) { SqlDbType = SqlDbType.Int };
            DataTable result = AAL.DAL.ExecuteQuery(sqlQry, sParamArray);
            if (result == null || result.Rows.Count == 0)
                btnAddProgram.Enabled = false;
            else
                btnAddProgram.Enabled = true;
        }

        private void btnAddProgram_Click(object sender, EventArgs e)
        {
            ShowProgramAdd();
        }

        private void ShowProgramAdd()
        {
            //Flip to the Add Program screen
            this.Parent.Controls.Add(new ucPrograms_Add() { MemberID = _MemberID, Dock = DockStyle.Fill });
            this.Parent.Controls.Remove(this);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string deletePlans = "Are you sure that you want to delete the following plan(s)?: " + Environment.NewLine;
            foreach (DataGridViewRow dgvr in grdPrograms.SelectedRows)
                deletePlans += dgvr.Cells["Description"].Value.ToString() + Environment.NewLine;

            DialogResult dr = MessageBox.Show(deletePlans,"NCRGEA - Delete Plan(s)", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                
                foreach (DataGridViewRow dgvr in grdPrograms.SelectedRows)
                {
                    if (!AAL.UAL.RemovePlan((int)dgvr.Cells["PlanID"].Value, _MemberID))
                    {
                        MessageBox.Show("An error occurred while removing this plan: '" + dgvr.Cells["Description"].Value.ToString() + "'! Please check the error log.");
                        break;
                    }
                }
                RefreshData();
            }
        }

        private void grdPrograms_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || grdPrograms.RowCount == 0 || !_IsEnrolled)
                return;

            //Flip to the Add Program screen
            this.Parent.Controls.Add(new ucPrograms_Edit() { MemberID = _MemberID, PlanID=(int)grdPrograms.Rows[e.RowIndex].Cells["PlanID"].Value, Dock = DockStyle.Fill });
            this.Parent.Controls.Remove(this);
        }
    }
}
